<template>
    <!-- 对话框表单 -->
    <!-- 建议使用 Prettier 格式化代码 -->
    <!-- el-form 内可以混用 el-form-item、FormItem、ba-input 等输入组件 -->
    <el-dialog
        class="ba-operate-dialog"
        :close-on-click-modal="false"
        :model-value="['Add', 'Edit'].includes(baTable.form.operate!)"
        @close="baTable.toggleForm"
        width="50%"
    >
        <template #header>
            <div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']" v-zoom="'.ba-operate-dialog'">
                {{ baTable.form.operate ? t(baTable.form.operate) : '' }}
            </div>
        </template>
        <el-scrollbar v-loading="baTable.form.loading" class="ba-table-form-scrollbar">
            <div
                class="ba-operate-form"
                :class="'ba-' + baTable.form.operate + '-form'"
                :style="'width: calc(100% - ' + baTable.form.labelWidth! / 2 + 'px)'"
            >
                <el-form
                    v-if="!baTable.form.loading"
                    ref="formRef"
                    @submit.prevent=""
                    @keyup.enter="baTable.onSubmit(formRef)"
                    :model="baTable.form.items"
                    label-position="right"
                    :label-width="baTable.form.labelWidth + 'px'"
                    :rules="rules"
                >
                    <FormItem :label="t('stock.stockmaster.stockid')" type="string" v-model="baTable.form.items!.stockid" prop="stockid" :placeholder="t('Please input field', { field: t('stock.stockmaster.stockid') })" />
                    <FormItem :label="t('stock.stockmaster.mbflag')" type="select" v-model="baTable.form.items!.mbflag" prop="mbflag" :data="{ content: { 'A': t('stock.stockmaster.mbflag A'), 'K': t('stock.stockmaster.mbflag K'), 'M': t('stock.stockmaster.mbflag M'), 'G': t('stock.stockmaster.mbflag G'), 'B': t('stock.stockmaster.mbflag B'), 'D': t('stock.stockmaster.mbflag D') } }" :placeholder="t('Please select field', { field: t('stock.stockmaster.mbflag') })" />
                    <FormItem :label="t('stock.stockmaster.barcode')" type="string" v-model="baTable.form.items!.barcode" prop="barcode" :placeholder="t('Please input field', { field: t('stock.stockmaster.barcode') })" />
                    <FormItem :label="t('stock.stockmaster.longdescription')" type="textarea" v-model="baTable.form.items!.longdescription" prop="longdescription" :input-attr="{ rows: 2 }" @keyup.enter.stop="" @keyup.ctrl.enter="baTable.onSubmit(formRef)" :placeholder="t('Please input field', { field: t('stock.stockmaster.longdescription') })" />
                    <FormItem :label="t('stock.stockmaster.description')" type="string" v-model="baTable.form.items!.description" prop="description" :placeholder="t('Please input field', { field: t('stock.stockmaster.description') })" />
                    <FormItem :label="t('stock.stockmaster.units')" type="remoteSelect" v-model="baTable.form.items!.units" prop="units" :input-attr="{ pk: 'cncerp_setup_unitsofmeasure.unitname', field: 'unitname', 'remote-url': '/admin/setup.Unitsofmeasure/index' }" :placeholder="t('Please select field', { field: t('stock.stockmaster.units') })" />
                    <FormItem :label="t('stock.stockmaster.decimalplaces')" type="number" prop="decimalplaces" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.decimalplaces" :placeholder="t('Please input field', { field: t('stock.stockmaster.decimalplaces') })" />
                    <FormItem :label="t('stock.stockmaster.categoryid')" type="remoteSelect" v-model="baTable.form.items!.categoryid" prop="categoryid" :input-attr="{ pk: 'cncerp_stock_stockcategory.categoryid', field: 'categorydescription', 'remote-url': '/admin/stock.Stockcategory/index' }" :placeholder="t('Please select field', { field: t('stock.stockmaster.categoryid') })" />
                    <FormItem :label="t('stock.stockmaster.actualcost')" type="number" prop="actualcost" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.actualcost" :placeholder="t('Please input field', { field: t('stock.stockmaster.actualcost') })" />
                    <FormItem :label="t('stock.stockmaster.lastcost')" type="number" prop="lastcost" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.lastcost" :placeholder="t('Please input field', { field: t('stock.stockmaster.lastcost') })" />
                    <FormItem :label="t('stock.stockmaster.materialcost')" type="number" prop="materialcost" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.materialcost" :placeholder="t('Please input field', { field: t('stock.stockmaster.materialcost') })" />
                    <FormItem :label="t('stock.stockmaster.labourcost')" type="number" prop="labourcost" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.labourcost" :placeholder="t('Please input field', { field: t('stock.stockmaster.labourcost') })" />
                    <FormItem :label="t('stock.stockmaster.overheadcost')" type="number" prop="overheadcost" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.overheadcost" :placeholder="t('Please input field', { field: t('stock.stockmaster.overheadcost') })" />
                    <FormItem :label="t('stock.stockmaster.lowestlevel')" type="number" prop="lowestlevel" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.lowestlevel" :placeholder="t('Please input field', { field: t('stock.stockmaster.lowestlevel') })" />
                    <FormItem :label="t('stock.stockmaster.eoq')" type="number" prop="eoq" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.eoq" :placeholder="t('Please input field', { field: t('stock.stockmaster.eoq') })" />
                    <FormItem :label="t('stock.stockmaster.pansize')" type="number" prop="pansize" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.pansize" :placeholder="t('Please input field', { field: t('stock.stockmaster.pansize') })" />
                    <FormItem :label="t('stock.stockmaster.shrinkfactor')" type="number" prop="shrinkfactor" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.shrinkfactor" :placeholder="t('Please input field', { field: t('stock.stockmaster.shrinkfactor') })" />
                    <FormItem :label="t('stock.stockmaster.controlled')" type="radio" v-model="baTable.form.items!.controlled" prop="controlled" :data="{ content: { '1': t('stock.stockmaster.controlled 1'), '0': t('stock.stockmaster.controlled 0') } }" :placeholder="t('Please select field', { field: t('stock.stockmaster.controlled') })" />
                    <FormItem :label="t('stock.stockmaster.serialised')" type="radio" v-model="baTable.form.items!.serialised" prop="serialised" :data="{ content: { '1': t('stock.stockmaster.serialised 1'), '0': t('stock.stockmaster.serialised 0') } }" :placeholder="t('Please select field', { field: t('stock.stockmaster.serialised') })" />
                    <FormItem :label="t('stock.stockmaster.perishable')" type="radio" v-model="baTable.form.items!.perishable" prop="perishable" :data="{ content: { '1': t('stock.stockmaster.perishable 1'), '0': t('stock.stockmaster.perishable 0') } }" :placeholder="t('Please select field', { field: t('stock.stockmaster.perishable') })" />
                    <FormItem :label="t('stock.stockmaster.volume')" type="number" prop="volume" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.volume" :placeholder="t('Please input field', { field: t('stock.stockmaster.volume') })" />
                    <FormItem :label="t('stock.stockmaster.grossweight')" type="number" prop="grossweight" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.grossweight" :placeholder="t('Please input field', { field: t('stock.stockmaster.grossweight') })" />
                    <FormItem :label="t('stock.stockmaster.netweight')" type="number" prop="netweight" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.netweight" :placeholder="t('Please input field', { field: t('stock.stockmaster.netweight') })" />
                    <FormItem :label="t('stock.stockmaster.taxcatid')" type="remoteSelect" v-model="baTable.form.items!.taxcatid" prop="taxcatid" :input-attr="{ pk: 'cncerp_setup_taxcategories.taxcatid', field: 'taxcatname', 'remote-url': '/admin/setup.Taxcategories/index' }" :placeholder="t('Please select field', { field: t('stock.stockmaster.taxcatid') })" />
                    <FormItem :label="t('stock.stockmaster.discountcategory')" type="string" v-model="baTable.form.items!.discountcategory" prop="discountcategory" :placeholder="t('Please input field', { field: t('stock.stockmaster.discountcategory') })" />
                    <FormItem :label="t('stock.stockmaster.nextserialno')" type="number" prop="nextserialno" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.nextserialno" :placeholder="t('Please input field', { field: t('stock.stockmaster.nextserialno') })" />
                    <FormItem :label="t('stock.stockmaster.lastcostupdate')" type="date" v-model="baTable.form.items!.lastcostupdate" prop="lastcostupdate" :placeholder="t('Please select field', { field: t('stock.stockmaster.lastcostupdate') })" />
                    <FormItem :label="t('stock.stockmaster.appendfile')" type="file" v-model="baTable.form.items!.appendfile" prop="appendfile" />
                    <FormItem :label="t('stock.stockmaster.discontinued')" type="switch" v-model="baTable.form.items!.discontinued" prop="discontinued" :data="{ content: { '1': 'discontinued 1', '0': 'discontinued 0' } }" />
                </el-form>
            </div>
        </el-scrollbar>
        <template #footer>
            <div :style="'width: calc(100% - ' + baTable.form.labelWidth! / 1.8 + 'px)'">
                <el-button @click="baTable.toggleForm()">{{ t('Cancel') }}</el-button>
                <el-button v-blur :loading="baTable.form.submitLoading" @click="baTable.onSubmit(formRef)" type="primary">
                    {{ baTable.form.operateIds && baTable.form.operateIds.length > 1 ? t('Save and edit next item') : t('Save') }}
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import { reactive, ref, inject } from 'vue'
import { useI18n } from 'vue-i18n'
import type baTableClass from '/@/utils/baTable'
import FormItem from '/@/components/formItem/index.vue'
import type { FormInstance, FormItemRule } from 'element-plus'
import { buildValidatorData } from '/@/utils/validate'

const formRef = ref<FormInstance>()
const baTable = inject('baTable') as baTableClass

const { t } = useI18n()

const rules: Partial<Record<string, FormItemRule[]>> = reactive({
    stockid: [buildValidatorData({ name: 'required', title: t('stock.stockmaster.stockid') })],
    mbflag: [buildValidatorData({ name: 'required', title: t('stock.stockmaster.mbflag') })],
    longdescription: [buildValidatorData({ name: 'required', title: t('stock.stockmaster.longdescription') })],
    description: [buildValidatorData({ name: 'required', title: t('stock.stockmaster.description') })],
    units: [buildValidatorData({ name: 'required', title: t('stock.stockmaster.units') })],
    decimalplaces: [buildValidatorData({ name: 'number', title: t('stock.stockmaster.decimalplaces') }), buildValidatorData({ name: 'required', title: t('stock.stockmaster.decimalplaces') })],
    categoryid: [buildValidatorData({ name: 'required', title: t('stock.stockmaster.categoryid') })],
    actualcost: [buildValidatorData({ name: 'required', title: t('stock.stockmaster.actualcost') })],
    lastcost: [buildValidatorData({ name: 'required', title: t('stock.stockmaster.lastcost') })],
    materialcost: [buildValidatorData({ name: 'required', title: t('stock.stockmaster.materialcost') })],
    labourcost: [buildValidatorData({ name: 'required', title: t('stock.stockmaster.labourcost') })],
    overheadcost: [buildValidatorData({ name: 'required', title: t('stock.stockmaster.overheadcost') })],
    lowestlevel: [buildValidatorData({ name: 'required', title: t('stock.stockmaster.lowestlevel') })],
    eoq: [buildValidatorData({ name: 'required', title: t('stock.stockmaster.eoq') })],
    pansize: [buildValidatorData({ name: 'required', title: t('stock.stockmaster.pansize') })],
    shrinkfactor: [buildValidatorData({ name: 'required', title: t('stock.stockmaster.shrinkfactor') })],
    controlled: [buildValidatorData({ name: 'required', title: t('stock.stockmaster.controlled') })],
    serialised: [buildValidatorData({ name: 'required', title: t('stock.stockmaster.serialised') })],
    perishable: [buildValidatorData({ name: 'required', title: t('stock.stockmaster.perishable') })],
    volume: [buildValidatorData({ name: 'required', title: t('stock.stockmaster.volume') })],
    grossweight: [buildValidatorData({ name: 'required', title: t('stock.stockmaster.grossweight') })],
    netweight: [buildValidatorData({ name: 'required', title: t('stock.stockmaster.netweight') })],
    taxcatid: [buildValidatorData({ name: 'required', title: t('stock.stockmaster.taxcatid') })],
    lastcostupdate: [buildValidatorData({ name: 'date', title: t('stock.stockmaster.lastcostupdate') })],
    discontinued: [buildValidatorData({ name: 'required', title: t('stock.stockmaster.discontinued') })],
})
</script>

<style scoped lang="scss"></style>
